package com.fly.core.aop.cglib;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor
{
    private Enhancer enhancer = new Enhancer();
    
    public Object getProxy(Class<?> clazz)
    {
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }
    
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy)
        throws Throwable
    {
        System.out.println("前置代理");
        Object result = methodProxy.invokeSuper(o, objects); // 通过代理类调用父类中的方法
        System.out.println("后置代理");
        return result;
    }
}